#!/bin/bash
OUTPUT_DIR="./bin"

# 要编译的目标平台列表
platforms=("linux/amd64" "darwin/amd64")

# Go 源文件
SOURCE_FILE="main.go"

# 创建输出目录
mkdir -p ${OUTPUT_DIR}

# 编译每个平台
for platform in "${platforms[@]}"
do
    # 提取 OS 和 ARCH
    IFS="/" read -r -a platform_split <<< "${platform}"
    GOOS=${platform_split[0]}
    GOARCH=${platform_split[1]}

    # 设置输出文件名
    output_name="${OUTPUT_DIR}/${GOOS}-${GOARCH}"

    # Windows 平台需要添加 .exe 扩展名
    if [ "${GOOS}" = "windows" ]; then
        output_name="${output_name}.exe"
    fi

    # 编译
    echo "Compiling for ${GOOS}/${GOARCH}..."
    env GOOS=${GOOS} GOARCH=${GOARCH} go build -ldflags '-w -s' -o ${output_name} ${SOURCE_FILE}

    if [ $? -ne 0 ]; then
        echo "error occurred for ${GOOS}/${GOARCH}"
    else
        echo "compiled for ${GOOS}/${GOARCH}"
    fi
done